package ${packageName};

import com.wuyan.web.base.helper.PageHelper;
import com.wuyan.web.base.helper.rep.RepPageData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import com.querydsl.core.BooleanBuilder;
import org.springframework.data.domain.Pageable;
import ${entity.packageName}.${entity.className};
import ${lastRenderResponse.repository.packageName}.${lastRenderResponse.repository.className};
import ${lastRenderResponse.form.packageName}.${lastRenderResponse.form.className};

<#list imports as import>
<#if import!="java.lang.Integer">
import ${import};
</#if>
</#list>

import java.util.Map;
import javax.transaction.Transactional;
import java.util.Optional;

/**
 * service for ${entity.className} generated by jpa-codegen
 * ${comments}
 *
 * @author ${author}
 * @date ${date}
 */

@Service
public class ${className} {

    @Autowired
    private ${lastRenderResponse.repository.className} ${lastRenderResponse.repository.className?uncap_first};

    /**
     * 创建实体
     *
     * @param form 表单
     * @return 实体对象
     */
    @Transactional
    public ${entity.className} create(${lastRenderResponse.form.className} form) {
        ${entity.className} ${entity.className?uncap_first} = ${entity.className}.builder().build();
        BeanUtils.copyProperties(form, ${entity.className?uncap_first});

        // TODO 业务逻辑
        // TODO 业务逻辑

        return ${lastRenderResponse.repository.className?uncap_first}.save(${entity.className?uncap_first});
    }

    /**
     * 删除实体
     * @param id 实体id
     */
    public void delete(${entity.id.className} id) {
        ${lastRenderResponse.repository.className?uncap_first}
                .findById(id)
                .ifPresent(${lastRenderResponse.repository.className?uncap_first}::delete);
    }

    /**
     * 更新实体
     *
     * @param form 表单
     * @param id      实体id
     * @return 实体对象
     */
    @Transactional
    public Optional<${entity.className}> update(${lastRenderResponse.form.className} form, ${entity.id.className} id) {
        return ${lastRenderResponse.repository.className?uncap_first}.findById(id)
                .map(${entity.className?uncap_first} -> {
                    BeanUtils.copyProperties(form, ${entity.className?uncap_first});
                    // TODO 业务逻辑
                    return ${lastRenderResponse.repository.className?uncap_first}.save(${entity.className?uncap_first});
                });
    }

    /**
     * 获取一个实体对象
     *
     * @param id 实体id
     * @return 实体对象
     */
    public Optional<${entity.className}> get(${entity.id.className} id) {
        return ${lastRenderResponse.repository.className?uncap_first}.findById(id);
    }

    /**
     * 分页接口
     *
     * @param booleanBuilder 分页查询条件
     * @param pageable       分页条件
     * @return 分页结果
     */
    public RepPageData<${entity.className}> page(BooleanBuilder booleanBuilder, Pageable pageable) {
        Page<${entity.className}> pageResult = ${lastRenderResponse.repository.className?uncap_first}.findAll(booleanBuilder,pageable);
        return PageHelper.pageResp(pageResult.getContent(),
                pageable,
                pageResult.getTotalElements());
    }

}